package com.ceprei.common.result;

import org.springframework.util.ObjectUtils;

import java.io.Serializable;

/**
 * api 数据返回类
 * @author zhou minghao
 * @date 2024/11/27
 * @description
 */
public class Result<T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    public Result(int code, String msg) {
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }


    public static Result success(){
        return Result.success(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static Result success(int code , String msg){
        return new Result<>(code,msg);
    }

    public static Result success(Object data){
        Result<Object> result = Result.success(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        if(ObjectUtils.isEmpty(data)){
            result.setData(data);
        }
        return result;
    }

    public static Result error(int code , String msg){
        Result<Object> result = new Result<>(code, msg);
        return result;
    }

    public static Result error(ResultCode resultCode){
        Result<Object> result = new Result<>(resultCode.getCode(), resultCode.getMsg());
        return result;
    }

    public static Result error(String msg){
        Result<Object> result = new Result<>(ResultCode.ERROR.getCode(), msg);
        return result;
    }


}
